<template>
  <div class="regHome">
    <el-form label-width="80px" :rules="regFromRules" :model="regForm" ref="regFormRef">
      <el-form-item label="昵称:" prop="username">
        <el-input autocomplete="off" v-model="regForm.username"></el-input>
      </el-form-item>
      <el-form-item label="账号:" prop="account">
        <el-input autocomplete="off" v-model="regForm.account"></el-input>
      </el-form-item>
      <el-form-item label="密码:" prop="password">
        <el-input autocomplete="off" v-model="regForm.password" show-password></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input
          autocomplete="off"
          v-model="regForm.phoneNumber"
          style="width: 180px"
        ></el-input>
        <el-button
          type="success"
          style="margin-left: 10px"
          @click="getCode"
          round
          :disabled="disable"
          >{{ s == 60 ? "获取验证码" : s + "秒后可发送" }}</el-button
        >
      </el-form-item>
      <el-form-item label="验证码" prop="code">
        <el-input autocomplete="off" v-model="regForm.code"></el-input>
      </el-form-item>
      <el-form-item label="头像:">
        <el-upload
          class="avatar-uploader"
          action="https://jsonplaceholder.typicode.com/posts/"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "regHome",
  components: {},
  data() {
    return {
      // 倒计时
      s: 60,
      // 倒计时是否可用
      disable: false,
      //注册表单存储
      regForm: {
        username: "",
        account: "",
        password: "",
        phoneNumber: "",
        code: "",
        src: "",
      },
      //注册的表单验证规则
      regFromRules: {
        username: [
          {
            required: true,
            message: "请输入你的名字",
            trigger: "blur",
          },
          {
            min: 1,
            max: 10,
            message: "长度在 1 到 10 个字符",
            trigger: "blur",
          },
        ],
        account: [
          {
            required: true,
            message: "请输入账号",
            trigger: "blur",
          },
          {
            min: 5,
            max: 10,
            message: "长度在 5 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            min: 5,
            max: 15,
            message: "长度在 5 到 15 个字符",
            trigger: "blur",
          },
        ],
        //验证手机号
        phoneNumber: [
          {
            required: true,
            message: "请输入手机号",
            trigger: "blur",
          },
          {
            min: 11,
            max: 11,
            message: "请输入正确手机号",
            trigger: "blur",
          },
        ],
        //验证码
        code: [
          {
            required: true,
            message: "请输入验证码",
            trigger: "blur",
          },
          {
            min: 1,
            max: 11,
            message: "请输入正确验证码",
            trigger: "blur",
          },
        ],
      },
      //头像地址
      imageUrl: "",
      dialogFormVisible: true,
    };
  },
  methods: {
    //重置表单
    reSet() {
      this.$refs.regFormRef.resetFields();
    },
    //注册预验证
    reg() {
      this.$refs.regFormRef.validate(async (valid) => {
        if (!valid) return;
        this.dialogFormVisible = true;
        const { data: res } = await this.$http.post("addUser", this.regForm);
        if (res.code === 202) {
          this.$message.success("注册成功");
          this.dialogFormVisible = false;
          this.$refs.regFormRef.resetFields();
        } else {
          this.$message.error("用户已存在，注册失败");
          this.$refs.regFormRef.resetFields();
        }
      });
    },
    //头像上传
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      console.log(res);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    //获取验证码
    getCode() {
      this.$http
        .get("sendSms", {
          params: {
            phoneNumber: this.regForm.phoneNumber,
          },
        })
        .then((res) => {
          if (res.data.code == 200) {
            const timer = setInterval(() => {
              if (this.s == 0) {
                this.disable = false;
                this.s = 60;
                clearInterval(timer);
                return;
              }
              this.s--;
              this.disable = true;
            }, 1000);
          } else {
            this.$message.error("gnm,爷不想写了");
          }
        });
    },
  },
};
</script>

<style scoped>
/* 头像上传样式 */

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover .avatar-uploader-icon {
  border-color: #409eff;
}

.avatar-uploader-icon {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
